package com.service.impl;

import com.dao.*;
import com.pojo.*;
import com.service.CheckinService;
import com.service.CheckoutService;
import com.util.UUIDUtils;
import com.vo.CheckinVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.Date;
import java.util.List;

/**
 * @author mornd
 * @date 2020/12/4 - 15:23
 */
@Service
@Transactional
public class CheckinServiceImpl implements CheckinService {
    @Autowired
    private CheckinDao checkinDao;
    @Autowired
    private OrdersDao ordersDao;
    @Autowired
    private RoomTypeDao roomTypeDao;
    @Autowired
    private RoomDao roomDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private CheckoutDao checkoutDao;
    /**
     * 查询入住信息列表
     *
     * @param checkinVo
     * @return
     */
    @Override
    public List<Checkin> findCheckinList(CheckinVo checkinVo) {
        return checkinDao.findCheckinList(checkinVo);
    }

    /**
     * 添加入住信息
     * @param checkin
     * @return
     */
    @Override
    public int addCheckin(Checkin checkin) {
        //设置状态为入住中
        checkin.setStatus(3);
        //设置创建时间
        checkin.setCreateDate(new Date());
        int count = checkinDao.addCheckin(checkin);
        if(count > 0){
            //删除预定管理中的房间信息
            ordersDao.deleteOrders(checkin.getOrdersId());
            /*修改预订订单信息(从已确认的状态2改成入住中3)
            Order order = new Order();
            order.setId(checkin.getOrdersId());
            order.setStatus(3);//3为入住中
            ordersDao.updateOrder(order);*/
            //修改房型信息(在原有的已入住数量的基础上+1)
            RoomType roomType = roomTypeDao.findRoomTypeById(checkin.getRoomTypeId());
            roomType.setLivedNum(roomType.getLivedNum() + 1);
            //调用修改房型的方法
            roomTypeDao.updateRoomType(roomType);

            //修改房间状态
            Room room = new Room();
            room.setId(checkin.getRoomId());
            room.setStatus(3);
            roomDao.updateRoom(room);
        }
        return count;
    }

    /**
     * 删除入住信息（退房）
     *
     * @param checkinId
     * @return
     */
    @Override
    public boolean deleteCheckin(Integer checkinId, Principal principal) {
        try {
            //根据id查询入住信息详情
            Checkin checkinById = checkinDao.findById(checkinId);
            int count = checkinDao.deleteCheckin(checkinId);
            if(count > 0){
                //添加退房记录
                Checkout checkout = new Checkout();
                checkout.setConsumePrice(checkinById.getPayPrice());
                checkout.setCreateDate(new Date());
                BeanUtils.copyProperties(checkinById,checkout);
                //创建人id
                SysUser loginUser = userDao.findUserInfoByUserName(principal.getName());
                checkout.setCreatedBy(loginUser.getId());
                checkoutDao.addCheckout(checkout);

                //修改房型信息(在原有的已入住数量的基础上-1)
                RoomType roomType = roomTypeDao.findRoomTypeById(checkinById.getRoomTypeId());
                roomType.setLivedNum(roomType.getLivedNum() - 1);//已入住数量-1
                roomType.setAvilableNum(roomType.getAvilableNum() + 1);//可用房间数+1
                roomType.setReservedNum(roomType.getReservedNum() - 1);//已预定数量-1
                //调用修改房型的方法
                roomTypeDao.updateRoomType(roomType);
                //修改房间信息
                Room roomById = roomDao.findRoomById(checkinById.getRoomId());
                roomById.setStatus(1);//1表示可预定
                roomDao.updateRoom(roomById);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
